﻿Public Class frmTinhTrang
    Public SoTinhTrang As Integer = 3
    Private da As DataTable
    Private Sub TrangChủToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrangChủToolStripMenuItem.Click
        Me.Close()
    End Sub
    Private Sub frmTinhTrang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        da = LoaiTinhTrangBUS.GetAllLoaiTinhTrangBUS()
        dgvLoaiTinhTrang.DataSource = da
    End Sub
    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        If (da.Rows.Count >= SoTinhTrang) Then
            MessageBox.Show("Số tình trạng đã đủ bạn không thể thêm")
        Else
            Dim t As LoaiTinhTrangDTO = New LoaiTinhTrangDTO()
            Try

                t.TenTinhTrang = txtTenLoaiTinhTrang.Text
                LoaiTinhTrangBUS.InsertLoaiTinhTrangBUS(t)
                dgvLoaiTinhTrang.DataSource = LoaiTinhTrangBUS.GetAllLoaiTinhTrangBUS()
            Catch ex As Exception
                MessageBox.Show("Lỗi")
            End Try
        End If
       

    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        If (dgvLoaiTinhTrang.SelectedRows.Count > 0) Then

            For i As Integer = 0 To (dgvLoaiTinhTrang.SelectedRows.Count - 1)
                Try
                  
                    LoaiTinhTrangBUS.DeleteLoaiTinhTrangBUS(Integer.Parse(dgvLoaiTinhTrang.SelectedRows(i).Cells(0).Value.ToString()))

                Catch ex As Exception
                    MessageBox.Show("Tình trạng đang được tham chiếu tới")
                End Try
            Next
            dgvLoaiTinhTrang.DataSource = LoaiTinhTrangBUS.GetAllLoaiTinhTrangBUS()

        End If

    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim t As LoaiTinhTrangDTO = New LoaiTinhTrangDTO()
        

                Try
                    t.MaTinhTrang = Integer.Parse(txtMaLoaiTinhTrang.Text)
                    t.TenTinhTrang = txtTenLoaiTinhTrang.Text
                    LoaiTinhTrangBUS.UpdateLoaiTinhTrangBUS(t)
                    dgvLoaiTinhTrang.DataSource = LoaiTinhTrangBUS.GetAllLoaiTinhTrangBUS()
                Catch ex As Exception
                    MessageBox.Show("Lỗi")
                End Try
          

    End Sub

    

    Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvLoaiTinhTrang.SelectionChanged
        If (dgvLoaiTinhTrang.SelectedRows.Count > 0 And dgvLoaiTinhTrang.SelectedRows.Count < 2) Then

            txtMaLoaiTinhTrang.Text = dgvLoaiTinhTrang.SelectedRows(0).Cells(0).Value.ToString()
            txtTenLoaiTinhTrang.Text = dgvLoaiTinhTrang.SelectedRows(0).Cells(1).Value.ToString()

        End If
    End Sub
End Class